/* CoralCube | Massively Multiplayer Online Role-Playing Game(MMORPG) Emulator.
|* 
|* Copyright (C) 2008-2010 TrinityCore <http://www.trinitycore.org/>
|*               2008-2010 CoralCube <http://www.CoralCube.com/>
|*
|* This program is free software. You can redistribute it and/or modify it under the terms of 
|* the GNU General Public License as published by the Free Software Foundation, either 
|* version 2 of the License.
|*
|* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, 
|* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
|* See the GNU General Public License for more details.
|*
|* You should have received a copy of the GNU General Public License along with this program. 
|* If not, see <http://www.gnu.org/licenses/>.
|*/

#include "Battlefield.h"
#include "BattlefieldMgr.h"
#include "Chat.h"

bool ChatHandler::HandleBattlefieldStart(const char *args)
{
    uint32 battleid = 0;
    char* battleid_str = strtok((char*)args, " ");
    if (!battleid_str)
        return false;

    battleid = atoi(battleid_str);

    Battlefield* Bf = sBattlefieldMgr.GetBattlefieldByBattleId(battleid);

    if (!Bf)
        return false;

    Bf->StartBattle();
    if (battleid == 1)
        SendGlobalGMSysMessage("Wintergrasp (Command start used)");
    return true;
}

bool ChatHandler::HandleBattlefieldEnd(const char *args)
{
    uint32 battleid = 0;
    char* battleid_str = strtok((char*)args, " ");
    if (!battleid_str)
        return false;

    battleid = atoi(battleid_str);

    Battlefield* Bf = sBattlefieldMgr.GetBattlefieldByBattleId(battleid);

    if (!Bf)
        return false;

    Bf->EndBattle(true);
    if (battleid == 1)
        SendGlobalGMSysMessage("Wintergrasp (Command stop used)");
    return true;
}

bool ChatHandler::HandleBattlefieldEnable(const char *args)
{
    uint32 battleid = 0;
    char* battleid_str = strtok((char*)args, " ");
    if (!battleid_str)
        return false;

    battleid = atoi(battleid_str);

    Battlefield* Bf = sBattlefieldMgr.GetBattlefieldByBattleId(battleid);

    if (!Bf)
        return false;

    if (Bf->GetEnable())
    {
        Bf->SetEnable(false);
        if (battleid == 1)
            SendGlobalGMSysMessage("Wintergrasp is disable");
    }
    else
    {
        Bf->SetEnable(true);
        if (battleid == 1)
            SendGlobalGMSysMessage("Wintergrasp is enable");
    }

    return true;
}

bool ChatHandler::HandleBattlefieldSwitch(const char *args)
{
    uint32 battleid = 0;
    char* battleid_str = strtok((char*)args, " ");
    if (!battleid_str)
        return false;

    battleid = atoi(battleid_str);

    Battlefield* Bf = sBattlefieldMgr.GetBattlefieldByBattleId(battleid);

    if (!Bf)
        return false;

    Bf->EndBattle(false);
    if (battleid == 1)
        SendGlobalGMSysMessage("Wintergrasp (Command switch used)");
    return true;
}

bool ChatHandler::HandleBattlefieldTimer(const char *args)
{
    uint32 battleid = 0;
    uint32 time = 0;
    char* battleid_str = strtok((char*)args, " ");
    if (!battleid_str)
        return false;
    char* time_str = strtok(NULL, " ");
    if (!time_str)
        return false;
    battleid= atoi(battleid_str);
    time = atoi(time_str);
    Battlefield* Bf = sBattlefieldMgr.GetBattlefieldByBattleId(battleid);

    if (!Bf)
        return false;
    Bf->SetTimer(time*IN_MILLISECONDS);
    Bf->SendInitWorldStatesToAll();
    if (battleid == 1)
        SendGlobalGMSysMessage("Wintergrasp (Command timer used)");
    return true;
}
